$(function(){
	$(".marking").on("click",function(){
		//作业id
		var wid = $(this).prev().val();
		//班级id
		var cid = $(this).next().val();
		var tabs = $("#tabs");
		var tab=$("#tabs").tabs('getSelected');
		 tabs.tabs('update',{   
				tab:tab,
				options: {
					closable:true,
					title: '批阅习题',
					 href:'homework/mark_student_work/'+wid+"/"+cid // the new content URL
				}
	  });
	 tab.panel('refresh', 'homework/mark_student_work/'+wid+"/"+cid);
	});
	//阅卷
	$(".Mark").on("click",function(){
		//作业id
		var wid = $(this).prev().val();
		//学生id
		var sid = $(this).next().val();
		var tabs = $("#tabs");
		var tab=$("#tabs").tabs('getSelected');
		 tabs.tabs('update',{   
				tab:tab,
				options: {
					closable:true,
					title: '批阅习题',
					 href:'homework/markingwork/'+wid+"/"+sid // the new content URL
				}
	  });
	 tab.panel('refresh', 'homework/markingwork/'+wid+"/"+sid);
	});
	//查看进行中的作业
	$(".seework").on("click",function(){
		//作业id
		var wid = $(this).prev().val();
		//学生id
		var sid = $(this).next().val();
		var tabs = $("#tabs");
		var tab=$("#tabs").tabs('getSelected');
		 tabs.tabs('update',{   
				tab:tab,
				options: {
					closable:true,
					title: '批阅习题',
					 href:'homework/seehomework/'+wid+"/"+sid // the new content URL
				}
	  });
	 tab.panel('refresh', 'homework/seehomework/'+wid+"/"+sid);
	});
	//查看已批阅的作业
	$(".seemarkwork").on("click",function(){
		//作业id
		var wid = $(this).prev().val();
		//学生id
		var sid = $(this).next().val();
		var tabs = $("#tabs");
		var tab=$("#tabs").tabs('getSelected');
		 tabs.tabs('update',{   
				tab:tab,
				options: {
					closable:true,
					title: '批阅习题',
					 href:'homework/seemarkwork/'+wid+"/"+sid // the new content URL
				}
	  });
	 tab.panel('refresh', 'homework/seemarkwork/'+wid+"/"+sid);
	});
	//返回作业界面
	$("#returnmark").on("click",function(){
		var tabs = $("#tabs");
		var tab=$("#tabs").tabs('getSelected');
		 tabs.tabs('update',{   
				tab:tab,
				options: {
					closable:true,
					title: '批阅习题',
					 href:'marking_work'// the new content URL
				}
	  });
	 tab.panel('refresh', 'marking_work');
	});
	//返回到班级界面
	$("#returnclass").on("click",function(){
		//作业id
		var wid = $(this).prev().val();
		//班级id
		var cid = $(this).next().val();
		var tabs = $("#tabs");
		var tab=$("#tabs").tabs('getSelected');
		 tabs.tabs('update',{   
				tab:tab,
				options: {
					closable:true,
					title: '批阅习题',
					 href:'homework/seehomework/'+wid+"/"+cid // the new content URL
				}
	  });
		 tab.panel('refresh', 'homework/mark_student_work/'+wid+"/"+cid);
	});
	//隐藏统计分数
	$("#grade").hide();
	
	$("#btn1,.btn1").click(function(){
		var btn = $(this);
		$("input[name=select_score]").each(function(){
			if($(this).val() != "0"){
				var s = $("input[name=sc]").val();
				$(this).val(s == "" ? btn.text() : s);
			}
		});
	});
	
	$("#btn2,.btn2").click(function(){
		var btn = $(this);
		$("input[name=judge_score]").each(function(){
			if($(this).val() != "0"){
				var s = $("input[name=dc]").val();
				$(this).val(s == "" ? btn.text() : s);
			}
		});
	});
	
	$("#btn3,.btn3").click(function(){
		var btn = $(this);
		$("input[name=program_score]").each(function(){
			if($(this).val() != "0"){
				var s = $("input[name=pc]").val();
				$(this).val(s == "" ? btn.text() : s);
			}
		});
	});
	$(".btn4").click(function(){
		var btn = $(this);
		var dataId = btn.attr("data-id");
		var txt = $("input[name=program_score][data-id=" + dataId + "]").val(btn.text());
	});
	
	$("#ongrade").click(function(){
		var grade = $("#grade");
		grade.show();
		var s = 0;
		$("input[name=select_score]").each(function(){
			var v = $(this).val();
			if(v == ""){
				v = 0;
			}
			s += parseFloat(v);
		});
		$("input[name=judge_score]").each(function(){
			var v = $(this).val();
			if(v == ""){
				v = 0;
			}
			s += parseFloat(v);
		});
		$("input[name=program_score]").each(function(){
			var v = $(this).val();
			if(v == ""){
				v = 0;
			}
			s += parseFloat(v);
		});
		grade.html(s);
		$("#txt_score").val(s);
	});
	
	$("select[id^=qi]").change(function(){
		var qid = this.dataset.id;
		var selected_value = $(this).find("optgroup option:selected").html();
		$("#ts_" + qid).val(selected_value);
	});
	
	$("#save_score").on("click",function(){
		if($("#txt_score").val() == ""){
			$.messager.alert('提示','还没有统计分数');
			return false;
		}
		//作业id
		var wordid = $(this).prev().val();
		//学生id
		var sid = $(this).next().val();
		var score = $("#txt_score").val();
		$.messager.confirm('提示','确认阅卷',function(r){
		    if (r){
		    	$("#savesoure").form('submit', {
                    url: 'homework/savesoure',
                    data: $("#savesoure").serialize(),success:function(success) {
                    	if(success){
    						//批阅成功跳回学生界面
    						//作业id
    						var wid = $("#chuti_id").val();
    						//班级id
    						var cid = $("#cla_id").val();
    						var tabs = $("#tabs");
    						var tab=$("#tabs").tabs('getSelected');
    						 tabs.tabs('update',{   
    								tab:tab,
    								options: {
    									closable:true,
    									title: '批阅习题',
    									 href:'homework/seehomework/'+wid+"/"+cid // the new content URL
    								}
    					  });
    						 tab.panel('refresh', 'homework/mark_student_work/'+wid+"/"+cid);
                    	}
                    }
                });
		    }
		});
	});
	$(".seenewwork").on("click",function(){
		//作业id
		var wid = $(this).prev().val();
		var tabs = $("#tabs");
		var tab=$("#tabs").tabs('getSelected');
		 tabs.tabs('update',{   
				tab:tab,
				options: {
					closable:true,
					title: '批阅习题',
					 href:'homework/seenewwork/'+wid// the new content URL
				}
	  });
	 tab.panel('refresh', 'homework/seenewwork/'+wid);
	});
	
	$("#qing_btn_sh").on("click",function(){
		//作业id
		var wid = $("#work_id").val();
		var extend = $("#extend").val();
		$.post("homework/updateentime/"+wid+"/"+extend, null, function(rs) {
			if (rs.success) {
				$.messager.alert("提示","修改成功");
				var tabs = $("#tabs");
				var tab=$("#tabs").tabs('getSelected');
				tabs.tabs('update',{   
						tab:tab,
						options: {
							closable:true,
							title: '批阅习题',
							 href:'homework/seenewwork/'+wid// the new content URL
						}
				 });
				 tab.panel('refresh', 'homework/seenewwork/'+wid);
		 } else {
				$.messager.alert("提示", rs.message);
			}
		});
	
	});
	$(".zhongzuo").on("click",function(){
		var wid = $(this).prev().val();
		var sid = $(this).next().val();
		$.messager.confirm('提示','确定要删除该考试成绩，并重新阅卷吗？',function(r){
		    if (r){
		    	$.post("homework/againmark/"+wid+"/"+sid, null, function(rs) {
				if (rs.success) {
					var tabs = $("#tabs");
					var tab=$("#tabs").tabs('getSelected');
					 tabs.tabs('update',{   
							tab:tab,
							options: {
								closable:true,
								title: '批阅习题',
								 href:'homework/markingwork/'+wid+"/"+sid // the new content URL
							}
				  });
				 tab.panel('refresh', 'homework/markingwork/'+wid+"/"+sid);
				} else {
					$.messager.alert("提示", rs.message);
				}
			});
		    }
		});
	});
	
	$(".redo").on("click",function(){
		var wid = $(this).prev().val();
		var cid = $(this).next().val();
		var jdid = $("#workjdid").val();
		$.messager.confirm('提示','你确定该操作',function(r){
		    if (r){
		    	$.post("homework/redowork/"+jdid, null, function(rs) {
				if (rs.success) {
					var tabs = $("#tabs");
					var tab=$("#tabs").tabs('getSelected');
					 tabs.tabs('update',{   
							tab:tab,
							options: {
								closable:true,
								title: '批阅习题',
								 href:'homework/mark_student_work/'+wid+"/"+cid // the new content URL
							}
				  });
				 tab.panel('refresh', 'homework/mark_student_work/'+wid+"/"+cid);
				} else {
					$.messager.alert("提示", rs.message);
				}
			});
		    }
		});
	});
	
});